package com.twu.biblioteca;

import java.util.ArrayList;

/**
 * Created by lauraionescu on 12/04/2014.
 */
public class Library {

    private ArrayList<User> userList = new ArrayList<User>();
    private User currentUser;
    private ArrayList<Item> itemList = new ArrayList<Item>();
    private ArrayList<Item> initialItemList = new ArrayList<Item>();


    public Library(){
        Book book1 = new Book("The Plague", "Albert Camus");
        itemList.add(book1);
        initialItemList.add(book1);

        Book book2 = new Book("The Castle", "Franz Kafka");
        itemList.add(book2);
        initialItemList.add(book2);

        Movie movie1 = new Movie("La Vita e Bella", 1997, "Roberto Benigni");
        itemList.add(movie1);
        initialItemList.add(movie1);

        Movie movie2 = new Movie("The Seventh Seal", 1957, "Ingmar Bergman");
        itemList.add(movie2);
        initialItemList.add(movie2);

        User user1 = new User("123-4567", "user1","Jane Smith", "jane@gmail.com", "+34 95683457");
        User user2 = new User("000-1234", "user3", "N/A", "N/A", "N/A");
        userList.add(user1);
        userList.add(user2);

    }

    public String displayBookList(){

        String bookListMessage = "";

        for (Book aBookList : getBookList()) {
            System.out.println(aBookList);
            bookListMessage += aBookList+"\n";
        }
        return bookListMessage;
    }


    public String displayMovieList() {

        String movieListMessage = "";

        for (Movie movie : getMovieList()) {
            System.out.println(movie);
            movieListMessage += movie + "\n";
        }
        return movieListMessage;
    }


    public String parseItemToCheckout(String userInput, boolean isBook){

        String checkoutResult;
        String[] itemDetails = userInput.split(", ");

        if (isBook) {


            if (itemDetails.length == 2) {
                Book toBeCheckedOut = new Book(itemDetails[0], itemDetails[1]);
                checkoutResult = checkout(toBeCheckedOut);
            } else {
                Book toBeCheckedOut = new Book("default", "default");
                checkoutResult = checkout(toBeCheckedOut);
            }

        } else {

            if (itemDetails.length == 3) {
                try {

                    Movie toBeCheckedOut = new Movie(itemDetails[0], Integer.parseInt(itemDetails[1]), itemDetails[2]);
                    checkoutResult = checkout(toBeCheckedOut);

                } catch (NumberFormatException ex){
                    checkoutResult = MainMenu.UNSUCCESSFUL_CHECKOUT;
                }

            } else {
                Movie toBeCheckedOut = new Movie("default", 0, "default");
                checkoutResult = checkout(toBeCheckedOut);

            }
        }

        return checkoutResult;
    }



    public String checkout(Object itemToCheckout){

        String checkoutStatusMessage = MainMenu.UNSUCCESSFUL_CHECKOUT;

        if (itemList.remove(itemToCheckout)){
            checkoutStatusMessage = MainMenu.SUCCESSFUL_CHECKOUT;
            initialItemList.get(initialItemList.indexOf(itemToCheckout)).setBorrower(currentUser);
        }

        System.out.print(checkoutStatusMessage);

        return checkoutStatusMessage;
    }

    public ArrayList<Book> getBookList() {
        ArrayList<Book> books = new ArrayList<Book>();
        for (Item item : itemList)
            if (item instanceof Book)
                books.add((Book) item);
        return books;
    }

    public ArrayList<Movie> getMovieList(){
        ArrayList<Movie> movies = new ArrayList<Movie>();
        for (Item item : itemList)
            if (item instanceof Movie)
                movies.add((Movie) item);
        return movies;
    }


    public String parseItemToCheckin(String userInput, boolean isBook) {

        String returnItemMessage;
        String[] itemDetails = userInput.split(", ");

        if (isBook && itemDetails.length == 2){
            Book toBeReturned = new Book(itemDetails[0], itemDetails[1]);
            returnItemMessage = checkin(toBeReturned);
        } else
            if (!isBook && itemDetails.length == 3){
                try {
                    Movie toBeReturned = new Movie(itemDetails[0], Integer.parseInt(itemDetails[1]), itemDetails[2]);
                    returnItemMessage = checkin(toBeReturned);
                } catch (NumberFormatException ex){
                    returnItemMessage = MainMenu.UNSUCCESSFUL_RETURN;
                }
            }
            else
                returnItemMessage = MainMenu.UNSUCCESSFUL_RETURN;

        return returnItemMessage;
    }


    public String checkin(Object toReturn) {

        String returnItemMessage = MainMenu.UNSUCCESSFUL_RETURN;

        if (initialItemList.contains(toReturn) && !itemList.contains(toReturn)) {
            if (toReturn instanceof Book)
                itemList.add((Book)toReturn);
            else
                itemList.add((Movie)toReturn);
            returnItemMessage = MainMenu.SUCCESSFUL_RETURN;
        }

        System.out.print(returnItemMessage);

        return returnItemMessage;

    }


    public boolean login(User registeredUser) {

        for (User user : userList){
            if (user.getPassword().equals(registeredUser.getPassword()) && user.getLogin().equals(registeredUser.getLogin())){
                currentUser = user;
                return true;
            }

        }
        return false;

    }

    public User getCurrentUser() {
        return currentUser;
    }

    public ArrayList<Item> getItemList(){
        return itemList;
    }

    public ArrayList<Item> getInitialItemList(){
        return initialItemList;
    }

}


